<?php
global $JoinYear;
global $PrevYear;
$Year = date("Y");
 $NYear = date("Y")+1; $PYear = date("Y")-1;
$JoinYear = $Year."-".$NYear;
$PrevYear = $PYear."-".$Year;
$path = drupal_get_path('module', 'document');
drupal_add_css($path . '/css/documents.css');
//drupal_add_js($path . '/js/documents.js');
  
function documentshome() {
global $language,$base_url;
 $url = $base_url . '/' . drupal_get_path('module', 'document');
 if($language->language=='ta'){
	$lang= '/ta';
 }else{
 	$lang='';
 }
	 $output = '<div class="rsslogo">' . get_rss_feed("documents_rssfeed") . '</div>	';
	 $output .= '<div id="viewmod_documents_'.$language->language.'" class="view-header">
		<div class="inner_tab_content">
			<div class="inner_li">
				<a class="view_head_sel" href="'.$base_url.$lang.'/documents/department_wise/">'.t('Department wise').'</a>		
			</div>
		    <div class="inner_li">				
				<a class="inactive"  href="'.$base_url.$lang.'/documents/category">'.t('Category Wise').'</a>
			</div>		
			<div class="inner_li">';
			if($language->language =='en'){		
		 		$output .= '<a class="inactive" href="'.$base_url.$lang.'/documents/atoz">'.t('A to Z').'</a>';
			}
			 $output .= '</div>		
		    <div class="inner_li">				
				<a class="inactive"  href="'.$base_url.$lang.'/documents/search">'.t('Search').'</a>
			</div>
		</div>		
		<div class="clear"></div>		
	</div>';

	my_db_change(1);
	$result = db_query("select DISTINCT dept_name,dep_id from depart_mst where dep_id in (select distinct(cast(field_department_value as integer)) as deptname
  from field_data_field_department)  ORDER BY dep_id");
	my_db_change(0);
	$op = "<div class='document_head_list'>";
	$st =1;
	foreach ($result as $dept) {
	 	if($st%2!=0) { 
			$cls = "first_clm";
		} 
		else {
			$cls = "second_clm";
		}
	
		 $op .= "<div class='".$cls." d".$dept->dep_id."'>";
		 $op .= "<img src='".$url."/images/icon_main.png' alt='More' /> "; 
		 $op .= l(trim(t($dept->dept_name)),$lang.'/documents/dept/'.$dept->dep_id)."</div>";	 
		 
		 $st++;

	}
	$op .="</div>";
	$output .= "<div class='result_inner'>";
	$output .= $op;
	$output .= "<div class='clr'></div></div>";	
	
return $output;


}

function documents_dept_data($depid) {
global $language,$base_url,$user,$JoinYear,$PrevYear,$NYear ;
//print $Year;
 $url = $base_url . '/' . drupal_get_path('module', 'documents');
 
 my_db_change(1); 
 $depid = arg(2);

	$query = new EntityFieldQuery();
	$query->entityCondition('entity_type', 'node')
	  ->entityCondition('bundle', 'documents')   
	  ->fieldCondition('field_department', 'value', $depid, '=')
	  ->fieldCondition('field_doc_year', 'value', array($JoinYear,$Year,$PrevYear), 'IN')
	  ->fieldOrderBy('field_doc_year','value','DESC')
	  ->propertyCondition('status',1)
	 //->propertyCondition('title','ASC')
	  ->propertyCondition('language', array($GLOBALS['language']->language, LANGUAGE_NONE), 'IN');
	 //->pager(15,0);
	  
	$result = $query->execute();
	
	
	$nid = current($result['node']);
	
	if (isset($result['node'])) {
	  $news_items_nids = array_keys($result['node']); 
	  	   //$items = node_load_multiple($news_items_nids, array('type' => 'documents'));
	}
	 my_db_change(0);
	 //$output = theme('documents_dep_profile_theme',array('items' => $items,'depid'=>$depid,'nid'=>$nid));
	// $output .= '</div>';
	if($language->language=='ta'){
		$lang= '/ta';
	}else{
	 $lang='';
	}
	$output .='<div class="view-header" id="viewmod_documents_'.$language->language.'"><div class="inner_tab_content">
			<div class = "inner_li"><a class="view_head_sel active" href="'.$base_url.$lang.'/documents/deptname">'.t('Department wise').'</a></div>		   	
			<div class = "inner_li"><a class="view_head_nonsel" href="'.$base_url.$lang.'/documents/category">'.t('Category wise').'</a></div>	
        	 <div class = "inner_li">';
	if($language->language=='en'){
	$output .= '<a class="view_head_nonsel" href="'.$base_url.$lang.'/documents/atoz">'.t(' A to Z').'</a>';
	}
	$output.='</div>		
		    <div class = "inner_li"><a class="view_head_nonsel" href="'.$base_url.$lang.'/documents/search">'.t('Search').'</a></div>
		</div>';
		
	$output.='<div class="result_inner"><div>';
	foreach ($news_items_nids as $nid){
			my_db_change(1);
			$vid = db_query('SELECT vid FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchField();
			$row = node_load($nid, $vid);
			$depname = $row->field_department['und'][0]['value'] ;
		//print "<pre>";print_r($items);print "</pre>";
	 if($depname != $dname){
	 	$dname =$depname;
		$head.='<div class="res_dept"><span >
			<strong>'.t(dept_name($depname)).'</strong></span>';
	  }
		$bk = '<span style="float:right;margin-right:23px;">
			<a href="'.$base_url.$lang.'/documents/deptname">'.t('Back').'</a></span>
		</div>';
		 if ($row->field_doctype['und'][0]['value'] == 'online') {
			$link =  $row->field_docupload['und'][0]['value'];
		}
		else if($row->field_doctype['und'][0]['value'] == 'download') {
		     $link = $row->field_docupload['und'][0]['uri'];		
		    $wrapper = file_stream_wrapper_get_instance_by_uri($link);
			if($wrapper == true){
				 $pathfile = $wrapper->getDirectoryPath() . "/" . file_uri_target($link);
			}
			  $link="'http://cms.tn.gov.in/tncms_latest/$pathfile'";
		}
		if ($row->field_docupload['und'][0]['filesize'] != '0') {
			$kb = ceil($row->field_docupload['und'][0]['filesize']/1024);
			$kbyte= $kb.KB;
			if($kb >1024) { 
			  $kb = ceil($kb/1024);
			  $kbyte= $kb.MB;
			}
		}
		$res .= ' <div class="res_title_ch" style="">
				 <p><a onclick="return tnportal_window('.$link .');" href="javascript:void(0);" >'. ucfirst(t($row->title)).'  <img alt="Download Icon" src="http://cms.tn.gov.in/sites/default/files/icons/pdf_icon.gif">
						<span class="kb">
							<span>('.$kbyte.')</span>
						</span>		
					</a>
				 </p>
			</div>	';		
		$i++; 
		}
		if($i == '0'){
			 $res .='<div class="no_record">No Results Found </div>';
		}
	$res .= '</div><div class="clr"></div></div>';
	 $yr .=archive_year();
	// $output .= theme('pager');
return $output.$head.$bk.$res.$yr;		
}

function documents_dept_archive_data(){
global $language,$base_url;
 $url = $base_url . '/' . drupal_get_path('module', 'document');
 $depid = arg(2);
 $currentyr = arg(3);
  if($language->language=='ta'){
	$lang= '/ta';
}else{
 $lang='';
}
	 $output = '<div id="viewmod_documents_'.$language->language.'" class="view-header">
		<div class="inner_tab_content">
			<div class="inner_li">
				<a class="view_head_sel" href="'.$base_url.$lang.'/documents/department_wise/">'.t('Department wise').'</a>		
			</div>
		    <div class="inner_li">				
				<a class="inactive"  href="'.$base_url.$lang.'/documents/category">'.t('Category Wise').'</a>
			</div>		
			<div class="inner_li">';
			if($language->language =='en'){		
		 		$output .= '<a class="inactive" href="'.$base_url.$lang.'/documents/atoz">'.t('A to Z').'</a>';
			}
			 $output .= '</div>			
		    <div class="inner_li">				
				<a class="inactive"  href="'.$base_url.$lang.'/documents/search">'.t('Search').'</a>
			</div>
		</div>';	
		
	$output .='<div class="result_inner"><div class="res_dept"><div class="arcyear">Archive of the '.$currentyr.'</div>';
	$output .='<span ><strong>'.t(dept_name($depid)).'</strong></span>';
	$output .='<span class="backbut"><a href="'.$base_url.$lang.'/documents/deptname">'.t('Back').'</a></span></div>';
	my_db_change(1);

	$query = new EntityFieldQuery();
	$query->entityCondition('entity_type', 'node')
	  ->entityCondition('bundle', 'documents')   
	  ->fieldCondition('field_department', 'value', $depid, '=')
	  ->fieldCondition('field_doc_year', 'value', $currentyr, '=')
	  ->fieldOrderBy('field_doc_year','value','DESC')
	  ->propertyCondition('status',1)
	  ->propertyCondition('language', array($GLOBALS['language']->language, LANGUAGE_NONE), 'IN');
	  
	$result = $query->execute();
	
	
	$nid = current($result['node']);
	
	if (isset($result['node'])) {
	  $items = array_keys($result['node']); 
	  
	 //  $items = node_load_multiple($news_items_nids, array('type' => 'documents'));
	}
	//print "<pre>";print_r($items);print "</pre>";
	my_db_change(0);
	$output .= theme('documents_archive_theme',array('items' => $items,'depid'=>$depid,'currentyr'=>$currentyr,'nid'=>$nid));
	$output .=archive_year();
	return $output;
}

function archive_year(){
global $language,$base_url,$user,$JoinYear,$PrevYear;
//print $JoinYear;
	$url = $base_url . '/' . drupal_get_path('module', 'documents');
	 if($language->language=='ta'){
		$lang= '/ta';
	}else{
	 	$lang='';
	} 
	my_db_change(1); 
	$depid = arg(2);
	$title = '';
	$query = new EntityFieldQuery();
	$query->entityCondition('entity_type', 'node')
	  ->entityCondition('bundle', 'documents')   
	//->fieldCondition('field_department', 'value', $depid, '=')
	//->fieldCondition('field_doc_year', 'value', array($JoinYear,$Year), 'IN')
	  ->fieldOrderBy('field_doc_year','value','DESC')
	  ->propertyCondition('status',1)
	  ->propertyCondition('language', array($GLOBALS['language']->language, LANGUAGE_NONE), 'IN');
	  
	$result = $query->execute();
	
	
	$nid = current($result['node']);
	
	if (isset($result['node'])) {
	  $news_items_nids = array_keys($result['node']); 
	  
	  $news_items  = node_load_multiple($news_items_nids, array('type' => 'documents'));
	}
	//print "<pre>";print_r($news_items);print "</pre>";
		my_db_change(0);
		$output .= '<div id="docArchive" style="text-align:right;"><span>Archive</span>';
		$i=0;
		foreach($news_items as $items){		
			if($title != $items->field_doc_year['und'][0]['value']) {
				$title = $items->field_doc_year['und'][0]['value'];
				$output .= '<a  style="padding: 10px 5px;" href="'.$base_url.$lang.'/documents/dept/'.$depid.'/'.$title.'">'.$title.'</a>';		
				if($i=='5' || $i == '10' || $i == '15'){
					$output .= '<div class="clr" style="padding-top: 10px;"></div>';
				}	
				$i++;	
			}			
		}	
	$output .='</div>';
	return $output;
}